package com.filmbooks.fragment.home.tab;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.filmbooks.R;
import com.filmbooks.adapter.home.tabs.WangyiAdapter;
import com.filmbooks.contract.home.tabs.WangyiContract;
import com.filmbooks.model.home.bean.home.WangyiNewsItemBean;
import com.filmbooks.presenter.home.tabs.WangyiPresenter;
import com.zyw.horrarndoo.sdk.base.BasePresenter;
import com.zyw.horrarndoo.sdk.base.fragment.BaseRecycleFragment;
import com.zyw.horrarndoo.sdk.utils.LogUtils;

import java.util.List;

import butterknife.BindView;

/**
 * Created by heshuxiang on 2018/3/1.
 * 描述：
 * 公司：盟将威
 * 作者：heshuxiang
 * 日期：2018/3/1.
 * 工程：FilmBooks
 * 包：com.filmbooks.fragment.home.tab
 */

public class WangyiFragment extends BaseRecycleFragment<WangyiContract.WangyiPresenter, WangyiContract.IWangyiModel> implements WangyiContract.IWangyiView, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.rv_wangyi)
    RecyclerView rv_wangyi;
    private WangyiAdapter mWangyiAdapter;

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return WangyiPresenter.newInstance();
    }

    @Override
    protected void onErrorViewClick(View view) {

    }


    @Override
    protected void showLoading() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_wangyi;
    }

    @Override
    public void initUI(View view, @Nullable Bundle savedInstanceState) {
        mWangyiAdapter = new WangyiAdapter(R.layout.item_recyle_home);
        rv_wangyi.setAdapter(mWangyiAdapter);
        rv_wangyi.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    public static WangyiFragment newInstance() {
        Bundle bundle = new Bundle();
        WangyiFragment zhiHuFragment = new WangyiFragment();
        zhiHuFragment.setArguments(bundle);
        return zhiHuFragment;
    }


    @Override
    public void updateContentList(List<WangyiNewsItemBean> list) {
        if (mWangyiAdapter.getData().size() == 0) {
            initRecycleView(list);
        } else {
            mWangyiAdapter.addData(list);
        }
    }


    private void initRecycleView(List<WangyiNewsItemBean> list) {
        mWangyiAdapter = new WangyiAdapter(R.layout.item_recyle_home, list);
        mWangyiAdapter.setOnLoadMoreListener(this, rv_wangyi);
        mWangyiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPresenter.onItemClick(position, (WangyiNewsItemBean) adapter.getItem(position));
            }
        });
        rv_wangyi.setAdapter(mWangyiAdapter);
    }

    @Override
    public void itemNotifyChanged(int position) {
        mWangyiAdapter.notifyItemChanged(position);
    }

    @Override
    public void showNetworkError() {
        mWangyiAdapter.setEmptyView(errorView);
    }

    @Override
    public void showLoadMoreError() {
        mWangyiAdapter.loadMoreFail();
    }

    @Override
    public void showNoMoreData() {
        mWangyiAdapter.loadMoreEnd(false);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.loadLatestList();
    }

    @Override
    public void onLoadMoreRequested() {
        mWangyiAdapter.loadMoreComplete();
        mPresenter.loadMoreList();
    }
}
